package com.sugar.psych.assessment.module.business.controller.admin.usertreeholechatrecord;

import com.sugar.psych.assessment.module.business.dal.dataobject.usertreeholechatrecord.UserTreeHoleChatDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.psych.assessment.framework.common.pojo.PageParam;
import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.framework.common.pojo.CommonResult;
import com.sugar.psych.assessment.framework.common.util.object.BeanUtils;
import static com.sugar.psych.assessment.framework.common.pojo.CommonResult.success;

import com.sugar.psych.assessment.framework.excel.core.util.ExcelUtils;

import com.sugar.psych.assessment.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.psych.assessment.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.psych.assessment.module.business.controller.admin.usertreeholechatrecord.vo.*;
import com.sugar.psych.assessment.module.business.dal.dataobject.usertreeholechatrecord.UserTreeHoleChatRecordDO;
import com.sugar.psych.assessment.module.business.service.usertreeholechatrecord.UserTreeHoleChatRecordService;

@Tag(name = "管理后台 - 树洞AI聊天会话")
@RestController
@RequestMapping("/business/user-tree-hole-chat-record")
@Validated
public class UserTreeHoleChatRecordController {

    @Resource
    private UserTreeHoleChatRecordService userTreeHoleChatRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建树洞AI聊天会话")
    @PreAuthorize("@ss.hasPermission('business:user-tree-hole-chat-record:create')")
    public CommonResult<Long> createUserTreeHoleChatRecord(@Valid @RequestBody UserTreeHoleChatRecordSaveReqVO createReqVO) {
        return success(userTreeHoleChatRecordService.createUserTreeHoleChatRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新树洞AI聊天会话")
    @PreAuthorize("@ss.hasPermission('business:user-tree-hole-chat-record:update')")
    public CommonResult<Boolean> updateUserTreeHoleChatRecord(@Valid @RequestBody UserTreeHoleChatRecordSaveReqVO updateReqVO) {
        userTreeHoleChatRecordService.updateUserTreeHoleChatRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除树洞AI聊天会话")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:user-tree-hole-chat-record:delete')")
    public CommonResult<Boolean> deleteUserTreeHoleChatRecord(@RequestParam("id") Long id) {
        userTreeHoleChatRecordService.deleteUserTreeHoleChatRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除树洞AI聊天会话")
                @PreAuthorize("@ss.hasPermission('business:user-tree-hole-chat-record:delete')")
    public CommonResult<Boolean> deleteUserTreeHoleChatRecordList(@RequestParam("ids") List<Long> ids) {
        userTreeHoleChatRecordService.deleteUserTreeHoleChatRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得树洞AI聊天会话")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:user-tree-hole-chat-record:query')")
    public CommonResult<UserTreeHoleChatRecordRespVO> getUserTreeHoleChatRecord(@RequestParam("id") Long id) {
        UserTreeHoleChatRecordDO userTreeHoleChatRecord = userTreeHoleChatRecordService.getUserTreeHoleChatRecord(id);
        return success(BeanUtils.toBean(userTreeHoleChatRecord, UserTreeHoleChatRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得树洞AI聊天会话分页")
    @PreAuthorize("@ss.hasPermission('business:user-tree-hole-chat-record:query')")
    public CommonResult<PageResult<UserTreeHoleChatRecordRespVO>> getUserTreeHoleChatRecordPage(@Valid UserTreeHoleChatRecordPageReqVO pageReqVO) {
        PageResult<UserTreeHoleChatRecordDO> pageResult = userTreeHoleChatRecordService.getUserTreeHoleChatRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserTreeHoleChatRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出树洞AI聊天会话 Excel")
    @PreAuthorize("@ss.hasPermission('business:user-tree-hole-chat-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserTreeHoleChatRecordExcel(@Valid UserTreeHoleChatRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserTreeHoleChatRecordDO> list = userTreeHoleChatRecordService.getUserTreeHoleChatRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "树洞AI聊天会话.xls", "数据", UserTreeHoleChatRecordRespVO.class,
                        BeanUtils.toBean(list, UserTreeHoleChatRecordRespVO.class));
    }

    // ==================== 子表（树洞AI聊天消息） ====================

    @GetMapping("/user-tree-hole-chat/page")
    @Operation(summary = "获得树洞AI聊天消息分页")
    @Parameter(name = "recordId", description = "记录ID")
    @PreAuthorize("@ss.hasPermission('business:user-tree-hole-chat-record:query')")
    public CommonResult<PageResult<UserTreeHoleChatDO>> getUserTreeHoleChatPage(PageParam pageReqVO,
                                                                                @RequestParam("recordId") Long recordId) {
        return success(userTreeHoleChatRecordService.getUserTreeHoleChatPage(pageReqVO, recordId));
    }

    @PostMapping("/user-tree-hole-chat/create")
    @Operation(summary = "创建树洞AI聊天消息")
    @PreAuthorize("@ss.hasPermission('business:user-tree-hole-chat-record:create')")
    public CommonResult<Long> createUserTreeHoleChat(@Valid @RequestBody UserTreeHoleChatDO userTreeHoleChat) {
        return success(userTreeHoleChatRecordService.createUserTreeHoleChat(userTreeHoleChat));
    }

    @PutMapping("/user-tree-hole-chat/update")
    @Operation(summary = "更新树洞AI聊天消息")
    @PreAuthorize("@ss.hasPermission('business:user-tree-hole-chat-record:update')")
    public CommonResult<Boolean> updateUserTreeHoleChat(@Valid @RequestBody UserTreeHoleChatDO userTreeHoleChat) {
        userTreeHoleChatRecordService.updateUserTreeHoleChat(userTreeHoleChat);
        return success(true);
    }

    @DeleteMapping("/user-tree-hole-chat/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除树洞AI聊天消息")
    @PreAuthorize("@ss.hasPermission('business:user-tree-hole-chat-record:delete')")
    public CommonResult<Boolean> deleteUserTreeHoleChat(@RequestParam("id") Long id) {
        userTreeHoleChatRecordService.deleteUserTreeHoleChat(id);
        return success(true);
    }

    @DeleteMapping("/user-tree-hole-chat/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除树洞AI聊天消息")
    @PreAuthorize("@ss.hasPermission('business:user-tree-hole-chat-record:delete')")
    public CommonResult<Boolean> deleteUserTreeHoleChatList(@RequestParam("ids") List<Long> ids) {
        userTreeHoleChatRecordService.deleteUserTreeHoleChatListByIds(ids);
        return success(true);
    }

	@GetMapping("/user-tree-hole-chat/get")
	@Operation(summary = "获得树洞AI聊天消息")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:user-tree-hole-chat-record:query')")
	public CommonResult<UserTreeHoleChatDO> getUserTreeHoleChat(@RequestParam("id") Long id) {
	    return success(userTreeHoleChatRecordService.getUserTreeHoleChat(id));
	}

}